import service from "@/utils/request";
import { defineStore } from "pinia";

export const userStore = defineStore("user", {
    state: () => {
        return {
            isLogin: false,
            token: null,
            userInfo: {},
        };
    },
    getters: {
        getLogin: (state) => {
            return state.isLogin;
        },
        getToken: (state) => {
            return state.token;
        },
        getUserName: (state) => {
            return state.userInfo.username;
        },
    },
    actions: {
        async setUserInfo() {
            await service.get("/getInfoByToken").then((res) => {
                // console.log(res.data.data);
                this.userInfo = res.data.data;
                sessionStorage.setItem("userInfo", JSON.stringify(this.userInfo));
            });
        },
        setToken(token) {
            this.token = token;
            sessionStorage.setItem("token", this.token);
        },
        setLogin() {
            service.get("/getInfoByToken").then((res) => {
                // console.log(res.data.data);
                this.isLogin = res.data.ok;
            });
        },
        destroyToken() {
            sessionStorage.removeItem("token");
            sessionStorage.removeItem("userInfo");
        },
    },
});
